<template>
  <div class="questionsContainer">
    <p>李嘉亮</p>
    <el-card class="box-card">
      <QuestionsForm @tableData="tableInfo" :question="1" :list="list" />

      <el-alert
        :title="'数据一共' + counts + '条'"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="120">
        </el-table-column>
        <el-table-column prop="subject" label="学科" width="106">
        </el-table-column>
        <el-table-column prop="catalog" label="目录" width="103">
        </el-table-column>
        <el-table-column label="题型" width="103">
          <template v-slot="{ row }">
            {{ row.questionType | numberTransformText(questionType) }}
          </template>
        </el-table-column>
        <el-table-column label="题干" width="280">
          <template v-slot="{ row }">
            {{ row.question | html2Text }}
          </template>
        </el-table-column>

        <el-table-column label="录入时间" width="180">
          <template v-slot="{ row }">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="难度" width="103">
          <template v-slot="{ row }">
            {{ row.difficulty | numberTransformText(difficulty) }}
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人" width="103">
        </el-table-column>
        <el-table-column prop="date" label="操作" width="180">
          <template v-slot="{ row }">
            <div>
              <el-row>
                <el-button
                  plain
                  size="small"
                  type="primary"
                  icon="el-icon-view"
                  circle
                  @click="preview(row)"
                ></el-button>
                <el-button
                  plain
                  size="small"
                  type="success"
                  icon="el-icon-edit"
                  @click="$router.push('/questions/new?id=' + row.id)"
                  circle
                ></el-button>
                <el-button
                  @click="removeRow(row)"
                  plain
                  size="small"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                ></el-button>
                <el-button
                  plain
                  size="small"
                  type="warning"
                  icon="el-icon-check"
                  @click="selection(row)"
                  circle
                ></el-button>
              </el-row>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <QuestionsPagination @pagination="list = $event" :counts="counts" />
    </el-card>
    <QuestionsDialog
      v-if="questionsVisible"
      :tableRow="tableRow"
      :questionsVisible.sync="questionsVisible"
    />
  </div>
</template>

<script>
import QuestionsPagination from '../components/questions/questions-pagination.vue'
import { remove, list, choiceAdd } from '@/api/hmmm/questions'
import QuestionsDialog from '../components/questions/questions-dialog.vue'
import QuestionsForm from '../components/questions/questions-form.vue'
import { questionType, difficulty } from '@/api/hmmm/constants'

export default {
  // 基础题库
  name: 'Questions',
  data () {
    return {
      tableData: [],
      counts: 0, // 共多少条
      difficulty, // 难度
      questionType, // 题型
      questionsVisible: false, // 弹框是否显示
      tableRow: {}, // 当前点击的行
      list: {
        page: 1,
        pagesize: 5
      },
      currentForm: {}
    }
  },
  components: {
    QuestionsForm,
    QuestionsDialog,
    QuestionsPagination
  },
  created () {
    this.loadQuestionsList()
  },
  methods: {
    async loadQuestionsList () {
      try {
        const { data: res } = await list({ ...this.list, ...this.currentForm })
        this.tableData = res.items
        this.counts = res.counts
      } catch (err) {
        console.log(err)
      }
    },
    // 预览弹框
    preview (row) {
      this.tableRow = row
      this.questionsVisible = true
    },
    // 删除
    removeRow (row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: row.id })
          this.$message.success('删除成功！')
          this.loadQuestionsList()
        })
        .catch(() => {})
    },
    // 加入精选
    selection (row) {
      // choiceState 1 加入 0移除
      // id
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await choiceAdd({
            choiceState: 1,
            id: row.id
          })
          this.$message.success('加入成功！')
          this.loadQuestionsList()
        })
        .catch(() => {})
    },
    tableInfo (val, currentForm) {
      this.tableData = val.items
      this.counts = val.counts
      this.currentForm = currentForm
    }
  },
  watch: {
    list: {
      deep: true,
      handler () {
        this.loadQuestionsList()
      }
    }
  }
}
</script>

<style scoped lang="less">
.questionsContainer {
  padding: 10px;
}
</style>
